********************************************************************************
******"Macroprudential Regulation, Quantitative Easing, and Bank Lending"*******
********************************************************************************

********************************************************************************
********************************************************************************
**********************************2019******************************************
********************************************************************************
********************************************************************************
global regcommands ", pool(5) compact absorb(bankid time#isin) vce(cluster bankid isin)"
global controls "postexlnassets lncrefin"

clear *
**# Bookmark (input)
use $inputdirectory/INB_preann2019m8
sum ext1r, d
scalar define s1=r(p25)
sum excashas, d
scalar define s2=r(p25)

**# Bookmark (input)
use $inputdirectory/INB_portfolio2019, clear
gen die=0
replace die=1 if inlist(staemitit,8,9,101,94,257,67,28,29,40,86,259,92,258,105,50,55,260,276,477,479,480,574,576,577,580,587,588,591,595,799,807,812,814,816,853,910,911,913,916,921,929,940,945,950,959,974,32)&inlist(setemi,100,704,770,173,706,708,121,120)&inlist(stanziabilita,23,24)&(monthtom>=15)&(monthtom<=375)&(monthtom!=.)&time==tm(2019m8)

order bankid time cod_isin book portfolio, first
keep bankid time cod_isin book portfolio die
rename cod_isin COD_ISIN

egen dielong=max(die), by(COD_ISIN)
keep if dielong==1

keep if portfolio==2

drop portfolio
collapse (sum) book, by(bankid time COD_ISIN)

**# Bookmark (input)
merge m:1 bankid using $inputdirectory/INB_preann2019m8, keepus(exassets)
keep if _merge==3|_merge==2
drop _merge
replace time=tm(2019m8) if time==.
drop exassets

fillin bankid COD_ISIN time
replace book=0 if book==.
egen isin=group(COD_ISIN)
drop if isin==.
egen id=group(bankid isin)
xtset id time

**# Bookmark (input)
merge m:1 bankid using $inputdirectory/INB_preann2019m8, keepus(e14 exlnassets excashas ext1r)
keep if _merge==3
drop _merge

**# Bookmark (input)
merge m:1 bankid time using $inputdirectory/INB_assets2019, keepus(assets)
keep if _merge!=2
drop _merge

**# Bookmark (input)
merge m:1 bankid time using $inputdirectory/INB_refinm2019, keepus(lncrefin)
keep if _merge!=2
drop _merge
recode lncrefin (.=0)

**# Bookmark (input)
merge m:1 time COD_ISIN using $inputdirectory/INB_isinsprices2019
keep if _merge!=2
drop _merge
replace price=. if price==0
replace priceavg=. if priceavg==0

gen quantity=book/price
xtset id time

gen qsold=quantity-L.quantity
gen sales=1000000*(qsold)*priceavg/L.assets

keep if time>=tm(2019m3)&time<=tm(2020m2)
egen xxx=mean(book), by(bankid isin)
sum xxx if xxx!=0, d
drop if abs(xxx)<=10000&xxx!=0
drop xxx
winsor2 sales if sales!=0&sales!=., suffix(_w) cut(1 99)
recode sales_w (.=0)

gen post=(time>=tm(2019m9))
gen poste14=post*e14
gen postexlnassets=post*exlnassets

foreach x in ext1r excashas {
gen `x'original=`x'
}
replace ext1r=ext1roriginal-scalar(s1)
replace excashas=excashasoriginal-scalar(s2)

foreach x in ext1r excashas {
gen post`x'=post*`x'
gen poste14`x'=post*`x'*e14
}

foreach x in excashas  {
gen post`x'ext1r=post*`x'*ext1r
gen poste14`x'ext1r=post*`x'*e14*ext1r
}

**# Bookmark (output - column 1)
cd $outputdirectory
est clear
reghdfe sales_w poste14 poste14ext1r poste14excashas poste14excashasext1r postexcashas  postext1r  postexcashasext1r $controls  $regcommands
est save TABIIIB_C12
cd ..


********************************************************************************
********************************************************************************
**********************************2015******************************************
********************************************************************************
********************************************************************************
global regcommands ", pool(5) compact absorb(bankid time#isin) vce(cluster bankid isin)"
global controls "dT7exlnassets lncrefin"

clear *
**# Bookmark (input)
use $inputdirectory/INB_preann2014m12
sum ext1r, d
scalar define s1=r(p25)
sum excashas, d
scalar define s2=r(p25)

**# Bookmark (input)
use $inputdirectory/INB_portfolio2015, clear
gen die=0
replace die=1 if inlist(staemitit,8,9,101,94,257,67,28,29,40,86,259,92,258,105,50,55,260,276,477,479,480,574,576,577,580,587,588,591,595,799,807,812,814,816,853,910,911,913,916,921,929,940,945,950,959,974)&inlist(setemi,100,704,770)&inlist(stanziabilita,23,24)&(monthtom>=27)&(monthtom<=375)&(monthtom!=.)&time==tm(2014m12)

order bankid time cod_isin book portfolio, first
keep bankid time cod_isin book portfolio die
rename cod_isin COD_ISIN

egen dielong=max(die), by(COD_ISIN)
keep if dielong==1

keep if portfolio==2

drop portfolio
collapse (sum) book, by(bankid time COD_ISIN)

**# Bookmark (input)
merge m:1 bankid using $inputdirectory/INB_preann2014m12, keepus(exassets)
keep if _merge==3|_merge==2
drop _merge
replace time=tm(2014m12) if time==.
drop exassets

fillin bankid COD_ISIN time
replace book=0 if book==.
egen isin=group(COD_ISIN)
drop if isin==.
egen id=group(bankid isin)
xtset id time

**# Bookmark (input)
merge m:1 bankid using $inputdirectory/INB_preann2014m12, keepus(e13 exlnassets excashas ext1r)
keep if _merge==3
drop _merge

**# Bookmark (input)
merge m:1 bankid time using $inputdirectory/INB_assets2015, keepus(assets)
keep if _merge!=2
drop _merge

**# Bookmark (input)
merge m:1 bankid time using $inputdirectory/INB_refinm2015, keepus(lncrefin)
keep if _merge!=2
drop _merge
recode lncrefin (.=0)

merge m:1 time COD_ISIN using $inputdirectory/INB_isinsprices2015
keep if _merge!=2
drop _merge
replace price=. if price==0
replace priceavg=. if priceavg==0

gen quantity=book/price
xtset id time

gen qsold=quantity-L.quantity
gen sales=1000000*(qsold)*priceavg/L.assets

keep if time>=tm(2014m7)&time<=tm(2015m6)
egen xxx=mean(book), by(bankid isin)
sum xxx if xxx!=0, d
drop if abs(xxx)<=10000&xxx!=0
drop xxx
winsor2 sales if sales!=0&sales!=., suffix(_w) cut(1 99)
recode sales_w (.=0)

run RO_tdummies 2015 1

foreach x in ext1r excashas {
gen `x'original=`x'
}
replace ext1r=ext1roriginal-scalar(s1)
replace excashas=excashasoriginal-scalar(s2)

run RO_tdintera 1 "e13 exlnassets excashas ext1r"

foreach x in ext1r excashas {
gen dT7e13`x'=dT7*`x'*e13
}

foreach x in excashas  {
gen dT7`x'ext1r=dT7*`x'*ext1r
gen dT7e13`x'ext1r=dT7*`x'*e13*ext1r
}

**# Bookmark (output - column 2)
cd $outputdirectory
est clear
reghdfe sales_w dT7e13 dT7e13ext1r dT7e13excashas dT7e13excashasext1r dT7excashas  dT7ext1r  dT7excashasext1r $controls  $regcommands
est save TABIIIB_C12,append
cd ..





